package com.melesta.engine;

import android.util.Log;
import com.melesta.payment.PaymentTransaction;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class DbBackup
{
  protected static final boolean SAVE_FILES_ON_DISK;

  // ERROR //
  static byte[] backupAll(DatabaseHelper paramDatabaseHelper)
  {
    // Byte code:
    //   0: new 18	java/io/ByteArrayOutputStream
    //   3: dup
    //   4: invokespecial 19	java/io/ByteArrayOutputStream:<init>	()V
    //   7: astore_1
    //   8: new 21	java/io/DataOutputStream
    //   11: dup
    //   12: aload_1
    //   13: invokespecial 24	java/io/DataOutputStream:<init>	(Ljava/io/OutputStream;)V
    //   16: astore_2
    //   17: aload_2
    //   18: aload_0
    //   19: invokestatic 27	com/melesta/engine/DbBackup:backupFiles	(Lcom/melesta/engine/DatabaseHelper;)[B
    //   22: invokestatic 31	com/melesta/engine/DbBackup:storeBytes	(Ljava/io/DataOutputStream;[B)V
    //   25: aload_2
    //   26: aload_0
    //   27: invokestatic 34	com/melesta/engine/DbBackup:backupTransactions	(Lcom/melesta/engine/DatabaseHelper;)[B
    //   30: invokestatic 31	com/melesta/engine/DbBackup:storeBytes	(Ljava/io/DataOutputStream;[B)V
    //   33: aload_1
    //   34: invokevirtual 38	java/io/ByteArrayOutputStream:toByteArray	()[B
    //   37: astore 7
    //   39: aload_2
    //   40: invokevirtual 41	java/io/DataOutputStream:close	()V
    //   43: aload_1
    //   44: invokevirtual 42	java/io/ByteArrayOutputStream:close	()V
    //   47: aload 7
    //   49: areturn
    //   50: astore 5
    //   52: aload 5
    //   54: invokestatic 48	com/melesta/engine/Error:processException	(Ljava/lang/Exception;)V
    //   57: aload_2
    //   58: invokevirtual 41	java/io/DataOutputStream:close	()V
    //   61: aload_1
    //   62: invokevirtual 42	java/io/ByteArrayOutputStream:close	()V
    //   65: aconst_null
    //   66: areturn
    //   67: astore 6
    //   69: aconst_null
    //   70: areturn
    //   71: astore_3
    //   72: aload_2
    //   73: invokevirtual 41	java/io/DataOutputStream:close	()V
    //   76: aload_1
    //   77: invokevirtual 42	java/io/ByteArrayOutputStream:close	()V
    //   80: aload_3
    //   81: athrow
    //   82: astore 4
    //   84: goto -4 -> 80
    //   87: astore 8
    //   89: aload 7
    //   91: areturn
    //
    // Exception table:
    //   from	to	target	type
    //   17	39	50	java/io/IOException
    //   57	65	67	java/lang/Exception
    //   17	39	71	finally
    //   52	57	71	finally
    //   72	80	82	java/lang/Exception
    //   39	47	87	java/lang/Exception
  }

  static byte[] backupFiles(DatabaseHelper paramDatabaseHelper)
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    paramDatabaseHelper.forEachFile(new DatabaseHelper.IDbFileProcessor()
    {
      public void process(String paramAnonymousString, byte[] paramAnonymousArrayOfByte)
        throws IOException
      {
        this.val$outWriter.writeUTF(paramAnonymousString);
        this.val$outWriter.writeInt(paramAnonymousArrayOfByte.length);
        this.val$outWriter.write(paramAnonymousArrayOfByte);
        Log.d("engine", "Backup-store(" + paramAnonymousString + ")");
      }
    });
    return localByteArrayOutputStream.toByteArray();
  }

  static byte[] backupTransactions(DatabaseHelper paramDatabaseHelper)
    throws IOException
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    DataOutputStream localDataOutputStream = new DataOutputStream(localByteArrayOutputStream);
    Iterator localIterator = paramDatabaseHelper.getTransactions().iterator();
    while (localIterator.hasNext())
    {
      byte[] arrayOfByte = ((PaymentTransaction)localIterator.next()).serialize();
      localDataOutputStream.writeInt(arrayOfByte.length);
      localDataOutputStream.write(arrayOfByte);
    }
    return localByteArrayOutputStream.toByteArray();
  }

  public static boolean restoreAll(DatabaseHelper paramDatabaseHelper, byte[] paramArrayOfByte)
  {
    if (paramArrayOfByte.length < 9)
      return false;
    try
    {
      DataInputStream localDataInputStream = new DataInputStream(new ByteArrayInputStream(paramArrayOfByte));
      byte[] arrayOfByte1 = new byte[localDataInputStream.readInt()];
      localDataInputStream.read(arrayOfByte1);
      restoreFiles(paramDatabaseHelper, arrayOfByte1);
      byte[] arrayOfByte2 = new byte[localDataInputStream.readInt()];
      localDataInputStream.read(arrayOfByte2);
      restoreTransactions(paramDatabaseHelper, arrayOfByte2);
      return true;
    }
    catch (Exception localException)
    {
      Error.processException(localException);
    }
    return false;
  }

  static void restoreFiles(DatabaseHelper paramDatabaseHelper, DataInputStream paramDataInputStream)
  {
    paramDatabaseHelper.deleteAllFiles();
    try
    {
      while (true)
      {
        String str = paramDataInputStream.readUTF();
        int i = paramDataInputStream.readInt();
        byte[] arrayOfByte = new byte[i];
        paramDataInputStream.read(arrayOfByte);
        Object[] arrayOfObject = new Object[2];
        arrayOfObject[0] = str;
        arrayOfObject[1] = Integer.valueOf(i);
        Log.d("engine", String.format("Backup-restore('%s', %d)", arrayOfObject));
        paramDatabaseHelper.saveFile(str, arrayOfByte);
      }
    }
    catch (IOException localIOException)
    {
    }
  }

  static void restoreFiles(DatabaseHelper paramDatabaseHelper, byte[] paramArrayOfByte)
  {
    restoreFiles(paramDatabaseHelper, new DataInputStream(new ByteArrayInputStream(paramArrayOfByte)));
  }

  static void restoreTransactions(DatabaseHelper paramDatabaseHelper, DataInputStream paramDataInputStream)
  {
    try
    {
      while (true)
      {
        byte[] arrayOfByte = new byte[paramDataInputStream.readInt()];
        paramDataInputStream.read(arrayOfByte);
        paramDatabaseHelper.saveTransaction(PaymentTransaction.deserialize(arrayOfByte));
      }
    }
    catch (IOException localIOException)
    {
    }
  }

  static void restoreTransactions(DatabaseHelper paramDatabaseHelper, byte[] paramArrayOfByte)
  {
    restoreTransactions(paramDatabaseHelper, new DataInputStream(new ByteArrayInputStream(paramArrayOfByte)));
  }

  private static void storeBytes(DataOutputStream paramDataOutputStream, byte[] paramArrayOfByte)
    throws IOException
  {
    paramDataOutputStream.writeInt(paramArrayOfByte.length);
    if (paramArrayOfByte.length > 0)
      paramDataOutputStream.write(paramArrayOfByte);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.melesta.engine.DbBackup
 * JD-Core Version:    0.6.2
 */